package chen;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import chen.valuebean.GoodsSingle;
import javax.servlet.http.HttpSession;

@WebServlet(name = "ListServlet", urlPatterns = {"/setList"})
public class ListServlet extends HttpServlet
{
    static ArrayList goodslist=new ArrayList();//用来存储商品
    static{												//静态代码块
	String[] names={"苹果","香蕉","梨","橘子"};//商品名称
	float[] prices={2.8f,3.1f,2.5f,2.3f};	   //商品价格
                    String[] urls = {"apple.jpg","banana.jpg","pear.jpg","orange.jpg"};
	for(int i=0;i<4;i++)
                 {   //初始化商品信息列表
	    //定义一个GoodsSingle类对象来封装商品信息			
	    GoodsSingle single=new GoodsSingle();
	    single.setName(names[i]); //封装商品名称信息
	    single.setPrice(prices[i]); //封装商品价格信息
	    single.setNum(1); //封装购买数量信息
                         single.setImgurl("images/"+urls[i]); //封装图片url
	    goodslist.add(i,single); //保存商品到goodslist集合对象中
	}	
  }
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        response.setContentType("text/html;charset=UTF-8");
	session.setAttribute("goodslist",goodslist); 		//保存商品列表到session中
	response.sendRedirect("Show.xhtml");			//跳转到show.xhtml页面显示商品
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}




